package com.controller;

import com.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.entity.Chapter;
import com.exception.ServiceException;
import com.github.pagehelper.PageInfo;
import com.service.ChapterService;
import com.util.AjaxResult;
import com.vo.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


import javax.servlet.http.HttpServletRequest;




@Controller
public class ChapterCtl {
    @Autowired
    ChapterService chapterService;
    @RequestMapping("/chapter/showChapterFront.do")
    public String showChapterFront(Integer course_id,HttpServletRequest request){
        String course_id1 = request.getParameter("course_id");
        Integer integer = Integer.valueOf(course_id1);
        System.out.println("11111111111111"+course_id1);
        return chapterService.selectChaptepByCourseId(integer,request);
    }

    @RequestMapping("/showFront_courseDetail.do")
    public ModelAndView showFrontcourseDetail(Integer course_id){
        System.out.println("courseId="+course_id);
        ModelAndView modelAndView = new ModelAndView("redirect:/chapter/showChapterFront.do");
        modelAndView.addObject("course_id",course_id);
        return modelAndView;
    }
    //吴彬彬  查询课程章节
    @RequestMapping("/chapter/loadChapter.do")
    @ResponseBody
    public AjaxResult loadChapter(ChapterVo chapterVo,HttpServletResponse response) throws IOException {
        System.out.println("1231321+"+chapterVo.toString());
        System.out.println("321321"+chapterVo.getPageNo());
        AjaxResult result = null;
        try{
            PageInfo info = chapterService.loadChapter(chapterVo);
            result = new AjaxResult(true,"上传成功",info);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }
        return result;
    }
    //吴彬彬  修改课程章节状态 简介  名字
    @RequestMapping("/chapter/modifyChapter.do")
    public AjaxResult modifyChapter(Chapter chapter){
        System.out.println(chapter.toString()+"123123123000");
        AjaxResult result = new AjaxResult(true,"成功",null);
        List<Chapter> chapters = chapterService.selectAllSTitle();
        for(Chapter x:chapters){
            if(!x.getTitle().equals(chapter.getTitle()) && !x.getInfo().equals(chapter.getInfo())){
                chapterService.modifyChapter(chapter);
                result.setMsg("成功");
                break;
            } else {
                throw new ServiceException("重复");
            }
        }
        return result;

    }
    //吴彬彬    增加
    @RequestMapping("/chapter/addChapter.do")
    public AjaxResult addChapter(Chapter chapter){
        AjaxResult result = new AjaxResult();
        List<Chapter> chapters = chapterService.selectAllSTitle();
        for(Chapter x:chapters){

            if(!x.getTitle().equals(chapter.getTitle()) && !x.getInfo().equals(chapter.getInfo())){
                   chapterService.addChapter(chapter);
                   result.setMsg("成功");
                    break;
            } else {
                    throw new ServiceException("重复");
            }
        }
        return result;
    }
    //吴彬彬   显示修改前的内容
    @RequestMapping("/chapter/showToModify.do")
    @ResponseBody
    public AjaxResult showToModify(Integer id){
        AjaxResult result = new AjaxResult(true,"上传成功",null);
        try{
            Chapter chapter = chapterService.showToModify(id);
            result.setObj(chapter);

        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }

        return result;
    }

}
